//index.js
//获取应用实例
var app = getApp()
var admin_id = app.globalData.admin_id;
var cat_id = 0;
var share = app.globalData.share;
Page({
	data: {
		allNum: 0,
		array: ['在线支付', '积分抵扣', '会员卡余额'],
		index: 0,
		tot_price: 0,
		int_money: 0,
		mail_price: 0,
		fin_price: 0,
		bz: '',
		cards: ['不使用'],
		cindex: 0,
		vipinfo: {
			integral: 0
		}
	},
	onLoad: function () {
		var that = this;
		console.log("管理员ID ====" + admin_id);
		//轮播分类ID
		// app.funs.banner(this,4731);
		that.setData({
			items: app.globalData.goods_info,
			intB: app.globalData.intB,
			sw_int: app.globalData.sw_int,
			sw_vip: app.globalData.sw_vip,
			sw_card: app.globalData.sw_card,
			vipinfo: app.globalData.vipinfo,
			mail_price: app.globalData.post_money,
		})

		var arrlist = ['在线支付']
		if (app.globalData.sw_int == 1) {
			arrlist.push('积分抵扣')
		}
		if (app.globalData.sw_vip == 1) {
			arrlist.push('会员卡余额')
		}

		that.setData({
			array: arrlist
		})

		var num = 0;
		var tot_price = 0;
		for (var i = 0; i < this.data.items.length; i++) {
			num += parseInt(this.data.items[i].num)
			tot_price += (parseFloat(this.data.items[i].shop_price) * parseInt(this.data.items[i].num))
		}

		that.setData({
			allNum: num,
			tot_price: getFloatStr(tot_price),
			int_money: (that.data.vipinfo.integral / that.data.intB).toFixed(2),
		})
		this.finPrice()
		wx.getStorage({
			key: 'address',
			success: function (res) {
				that.setData({
					address: res.data
				})
			}
		})

		if (app.globalData.sw_card == 1) {
			wx.request({
				url: 'https://xcxu.we-fs.com/index.php/Api/coupon/lists?admin_id=' + admin_id + '&openid=' + app.globalData.openid,
				success: function (e) {
					console.log(e)
					// var arr = e.data.filter(that.filtercard)
					that.setData({
						card: e.data.filter(that.filtercard)
					})
					var ass = ['不使用']
					for (var i = 0; i < that.data.card.length; i++) {
						ass.push(that.data.card[i].name)
					}
					that.setData({
						cards: ass
					})
				}
			})
		}
	},
	filterHeaderCatid: function (e) {
		return (e.cat_id == cat_id)
	},
	filtercard: function (e) {
		return (parseFloat(e.condition) <= parseFloat(this.data.tot_price))
	},
	cheageAddress: function (e) {
		var that = this;
		wx.chooseAddress({
			success: function (re) {
				console.log(re)
				that.setData({
					address: re
				})
				wx.setStorage({
					key: 'address',
					data: re,
				})
			}
		})
	},
	bindPickerChange: function (e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			index: e.detail.value
		})
		this.finPrice()
	},
	bindPickerChange1: function (e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			cindex: e.detail.value
		})
		this.finPrice()
	},
	getNowFormatDate: function () {
		var date = new Date();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = date.getFullYear() + month + strDate + date.getHours() + date.getMinutes() + date.getSeconds();
		return currentdate;
	},
	payOrder: function () {
		var that = this;
		console.log('支付')
		if (!that.data.address) {
			app.showPop('请填写地址')
			return false
		} else {
			var allName = '';
			for (var i = 0; i < that.data.items.length; i++) {
				allName += (that.data.items[i].goods_name + '*' + that.data.items[i].num + ' ')
			}

			if (that.data.array[that.data.index] != '会员卡余额') {

				if (that.data.fin_price == 0) {
					wx.showModal({
						title: '提示',
						content: '是否换购',
						success: function (com) {
							if (com.confirm) {
								wx.showLoading({
									title: '支付中...',
								})
								for (var i = 0; i < that.data.items.length; i++) {
									wx.request({
										url: 'https://xcxu.we-fs.com/index.php/api/goods/order1',
										method: 'post',
										data: {
											'name': that.data.address.userName,
											'sj': that.data.address.telNumber,
											'dz': that.data.address.provinceName + that.data.address.cityName + that.data.address.countyName + that.data.address.detailInfo + ' [备注] ' + that.data.bz,
											'admin_id': admin_id,
											'goods_id': that.data.items[i].goods_id,
											'goods_name': '【换购】' + that.data.items[i].goods_name + " " + that.data.items[i].spe + "*" + that.data.items[i].num,
											'num': that.data.items[i].num,
											'price': 0,
											'order_sn': that.getNowFormatDate(),
											'openid': app.globalData.openid
										},
										header: {
											'content-type': 'application/x-www-form-urlencoded'
										},
										success: function (res) {
											that.setData({
												list: res.data[0],

											})
											setTimeout(function () {
												that.setData({
													loadingHidden: true
												})
												wx.reLaunch({
													url: '../../pages/index/index',
												})
											}, 2000)
										},
										complete: function () {
											setTimeout(function () {
												wx.hideLoading()
											}, 2000)
										}
									})
								}

								// 花费积分
								app.opInt(that.data.cost_int, 'integral', 2, function () {
									console.log('积分减')
									setTimeout(function () {
										that.setData({
											loadingHidden: true
										})

									}, 2000)
								})


								//核销优惠券
								if (that.data.sw_card == 1) {
									if (that.data.cindex != 0) {
										wx.request({
											url: 'https://xcxu.we-fs.com/index.php/Api/coupon/uses?admin_id=' + admin_id + '&id=' + that.data.card[that.data.cindex - 1].id,
											success: function (e) {
												console.log(e)
												console.log('核销优惠券')
											}
										})
									}
								}
							}
						}
					})
					return false
				}

				app.payFor(that.data.address.userName, that.data.address.telNumber, that.data.fin_price, allName, function () {
					for (var i = 0; i < that.data.items.length; i++) {
						wx.request({
							url: 'https://xcxu.we-fs.com/index.php/api/goods/order1',
							method: 'post',
							data: {
								'name': that.data.address.userName,
								'sj': that.data.address.telNumber,
								'dz': that.data.address.provinceName + that.data.address.cityName + that.data.address.countyName + that.data.address.detailInfo + ' [备注] ' + that.data.bz,
								'admin_id': admin_id,
								'goods_id': that.data.items[i].goods_id,
								'goods_name': that.data.items[i].goods_name + " " + that.data.items[i].spe + "*" + that.data.items[i].num,
								'num': that.data.items[i].num,
								'price': that.data.items[i].shop_price * that.data.items[i].num,
								'order_sn': that.getNowFormatDate(),
								'openid': app.globalData.openid,
								'addAct': 'shareShop'
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: function (res) {
								that.setData({
									list: res.data[0],

								})
								setTimeout(function () {
									that.setData({
										loadingHidden: true
									})
									wx.reLaunch({
										url: '../../pages/index/index',
									})
								}, 2000)
							}
						})
					}

					// 花费积分
					app.opInt(that.data.cost_int, 'integral', 2, function () {
						console.log('积分减')
					})
					// 获得积分
					app.opInt(parseInt(that.data.fin_price), 'integral', 1, function () {
						console.log('积分加')
					})

					if (that.data.sw_card == 1) {
						if (that.data.cindex != 0) {
							wx.request({
								url: 'https://xcxu.we-fs.com/index.php/Api/coupon/uses?admin_id=' + admin_id + '&id=' + that.data.card[that.data.cindex - 1].id,
								success: function (e) {
									console.log(e)
									console.log('核销优惠券')
								}
							})
						}
					}
				})
			} else {
				wx.showModal({
					title: '提示',
					content: '支付￥' + that.data.fin_price + ',确认支付?',
					success: function (com) {
						if (com.confirm) {
							if (parseFloat(that.data.vipinfo.user_money) < parseFloat(that.data.fin_price)) {
								app.showPop('余额不足', false)
								return false
							}
							// 花费余额
							wx.showLoading({
								title: '支付中...',
							})
							app.opInt(parseFloat(that.data.fin_price), 'user_money', 2, function () {
								console.log('余额减')
								for (var i = 0; i < that.data.items.length; i++) {
									wx.request({
										url: 'https://xcxu.we-fs.com/index.php/api/goods/order1',
										method: 'post',
										data: {
											'name': that.data.address.userName,
											'sj': that.data.address.telNumber,
											'dz': that.data.address.provinceName + that.data.address.cityName + that.data.address.countyName + that.data.address.detailInfo + ' [备注] ' + that.data.bz,
											'admin_id': admin_id,
											'goods_id': that.data.items[i].goods_id,
											'goods_name': that.data.items[i].goods_name + " " + that.data.items[i].spe + "*" + that.data.items[i].num,
											'num': that.data.items[i].num,
											'price': that.data.items[i].shop_price * that.data.items[i].num,
											'order_sn': that.getNowFormatDate(),
											'openid': app.globalData.openid,
											'addAct': 'shareShop'
										},
										header: {
											'content-type': 'application/x-www-form-urlencoded'
										},
										success: function (res) {
											that.setData({
												list: res.data[0],
											})
											setTimeout(function () {
												wx.reLaunch({
													url: '../../pages/index/index',
												})
											}, 2000)
										},
										complete: function () {
											setTimeout(function () {
												wx.hideLoading()
											}, 2000)
										}
									})
								}
								// 花费积分
								app.opInt(that.data.cost_int, 'integral', 2, function () {
									console.log('积分减')
								})
								// 获得积分
								app.opInt(parseInt(that.data.fin_price), 'integral', 1, function () {
									console.log('积分加')
								})

								if (that.data.sw_card == 1) {
									if (that.data.cindex != 0) {
										wx.request({
											url: 'https://xcxu.we-fs.com/index.php/Api/coupon/uses?admin_id=' + admin_id + '&id=' + that.data.card[that.data.cindex - 1].id,
											success: function (e) {
												console.log(e)
												console.log('核销优惠券')
											}
										})
									}
								}
							})
						}
					}
				})
			}
		}
	},
	finPrice: function () {
		if (this.data.index == 0) {
			this.setData({
				fin_price: (parseFloat(this.data.tot_price) + parseFloat(this.data.mail_price)).toFixed(2),
				cost_int: 0
			})
		} else {
			if (parseFloat(this.data.int_money) >= parseFloat(this.data.tot_price)) {
				this.setData({
					fin_price: parseFloat(this.data.mail_price),
					cost_int: parseFloat(this.data.tot_price) * parseFloat(this.data.intB)
				})
			} else {
				this.setData({
					fin_price: (parseFloat(this.data.tot_price) + parseFloat(this.data.mail_price) - parseFloat(this.data.int_money)).toFixed(2),
					cost_int: parseFloat(this.data.int_money) * parseFloat(this.data.intB)
				})
			}
		}
		if (this.data.cindex != 0) {
			this.setData({
				fin_price: (parseFloat(this.data.fin_price) - parseFloat(this.data.card[this.data.cindex - 1].money)).toFixed(2)
			})
		}
	},
	inputs: function (e) {
		console.log(e.detail.value)
		this.setData({
			bz: e.detail.value
		})
	}
})


function getFloatStr(num) {
	num += '';
	num = num.replace(/[^0-9|\.]/g, '');
	if (/^0+/)
		num = num.replace(/^0+/, '');
	if (!/\./.test(num))
		num += '.00';
	if (/^\./.test(num))
		num = '0' + num;
	num += '00';
	num = num.match(/\d+\.\d{2}/)[0];
	return num
}